Introduction: Dynamic Elements

WebObjects' dynamic elements a bridge between scripted behavior (or behavior defined in compiled code) and the HTML page. A dynamic element can serve as a bridge by being able to:

Consider the WORepetition dynamic element. A WORepetition can take its values from the results of a database query and then generate the HTML tags (<UL>, <LI>, etc.) to display the data in a list:

The HTML for this page comes in part from statically declared elements (the heading, for example) and in part from dynamically generated ones (such as each item in the list). The HTML template file for this page shows how the dynamic elements (identified by the tag WEBOBJECT) and the static elements are intermixed:

    <HTML>
	<H3>Top Selling Items as of 5PM</H3>
	<OL> 
	<WEBOBJECT NAME = "ITEMS"> 
	    <LI><WEBOBJECT NAME = "ITEMNAME"></WEBOBJECT></LI>
	</WEBOBJECT>
	</UL>
    </HTML>

The corresponding declarations file associates each WEBOBJECT tag in the HTML template with a particular dynamic elements and indicates how the element's attributes will be initialized:

    ITEMS: WORepetition {list=findTopSellers; item=anItem};
    ITEMNAME: WOString {value=anItem};

Finally, a script file specifies how values used to initialize a dynamic object's attributes (such as the repetition's list) are derived. For example,

    id topSellers;
    
    - findTopSellers 
    {
	topSellers = /* message to fetch names of top sellers */;
    }

How Dynamic Elements are Initialized

A dynamic element requires information from the HTML template file, declarations file, and script file to be fully functional, as discussed above. The HTML template file establishes the location of the dynamic element and may provide a template representation for it in the HTML page. If the template file doesn't specify the representation of a dynamic element, the control can create one of its own. For example, in this HTML template the HTML tags in bold are optional:

    <HTML>
    <WEBOBJECT NAME="MYFORM">
    <FORM>
	    Your Name: 
	    <WEBOBJECT NAME="TEXTFIELD">
		    <INPUT TYPE="text"></WEBOBJECT>
	    <INPUT TYPE="submit">
    </FORM>
    </WEBOBJECT>
    </HTML>

MYFORM is a WOForm object and TEXTFIELD is a WOTextField object, as specified in the declarations file:

    MYFORM: WOForm {action = someAction};
    INPUTFIELD: WOTextField {value = textFieldValue};
Although the HTML tags are optional, it's best to include them in the template file so that, when the template is viewed in a Web Browser or other HTML rendering tool, it looks as much as possible like the dynamic page that will be generated at runtime.

The declarations file specifies how a dynamic element's attributes will be initialized. For example, this declaration indicates that the dynamic element named TEXTFIELD is a WOTextField object whose input will be returned in the variable "textFieldValue".

    INPUTFIELD: WOTextField {value=textFieldValue};
Each dynamic element declares a set of attributes for you to initialize. However, you can also specify additional attributes and their values within a dynamic element's declaration:

    TEXTFIELD: WOTextField {value = textFieldValue; size = 4};
In this case, only the value attribute is required by the WOTextField object. When a dynamic element is asked to produce its HTML representation, these additional attributes and values are simply copied into the HTML stream. The values for these additional attributes can be derived dynamically, just as with the built-in attributes.